//
//  BDDatePickerView.m
//  GetWebText
//
//  Created by wen on 2017/8/8.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopDatePickerView.h"

@interface BDShopDatePickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSInteger yearNum;
    NSInteger lastMon;
}
@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIToolbar *pickerToolBar;

@property (nonatomic, copy) void(^finishBlock)(NSInteger aYear, NSInteger aMonth);

@end

@implementation BDShopDatePickerView

+ (instancetype)datePick{
    return [[BDShopDatePickerView alloc] initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame{
    frame = [UIScreen mainScreen].bounds;
    if (self = [super initWithFrame:frame]) {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        [calendar setTimeZone: [NSTimeZone localTimeZone]];
        NSCalendarUnit calendarUnit = NSCalendarUnitYear|NSCalendarUnitMonth;
        NSDateComponents *startComponents = [calendar components:calendarUnit fromDate:[NSDate date]];
        yearNum = startComponents.year - 2016;
        lastMon = startComponents.month;
    }
    return self;
}

- (void)showWithYear:(NSInteger)year month:(NSInteger)month finishBlock:(void(^)(NSInteger aYear, NSInteger aMonth))finishBlock{
    self.finishBlock = finishBlock;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.contentView.top = self.height;
    [self.pickView selectRow:year - 2017 inComponent:0 animated:NO];
    [self.pickView selectRow:month inComponent:2 animated:NO];
    [UIView animateWithDuration:0.3 animations:^{
        self.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        self.contentView.bottom = self.height;
        
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark UIPickerViewDataSource 数据源方法
// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

// 返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return yearNum;
    }else if (component == 1) {
        return 0;
    }else if (component == 2) {
        NSInteger row = [pickerView selectedRowInComponent:0];
        if (row == yearNum - 1) {
            return lastMon + 1;
        }
        return 13;
    }
    return 13;
}

#pragma mark UIPickerViewDelegate 代理方法

// 返回每行的标题
//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    if (component == 0) {
//        return ASLocalizedString(@"年年年年");
//    }else if (component == 2) {
//        return ASLocalizedString(@"月月月月");
//    }else {
//        return @"";
//    }
//}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *myView = nil;
    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 60, 30)];
    //设置字体居中
    myView.textAlignment = NSTextAlignmentCenter;
    //这里currentArray是数据源
    if (component == 0) {
        myView.text = [NSString stringWithFormat:ASLocalizedString(@"%@年"), @(row + 2017)];
    }else if (component == 2) {
        if (row == 0) {
            myView.text = ASLocalizedString(@"整年");
        }else{
            myView.text = [NSString stringWithFormat:ASLocalizedString(@"%@月"), @(row)];
        }
    }else {
        myView.text =@"";
    }
    myView.font = [UIFont systemFontOfSize:18];         //用label来设置字体大小
    myView.backgroundColor = [UIColor clearColor];
    [myView sizeToFit];
    return myView;
}

- (void)closePickView{
    NSInteger newYear = [self.pickView selectedRowInComponent:0] + 2017;
    NSInteger newMonth = [self.pickView selectedRowInComponent:2];
    if (self.finishBlock) {
        self.finishBlock(newYear, newMonth);
    }
    [self hidden];
}

- (void)hidden{
    [UIView animateWithDuration:0.3 animations:^{
        self.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        self.contentView.top = self.height;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UIView *)bkView{
    if (!_bkView) {
        self.bkView = [[UIView alloc] initWithFrame:self.bounds];
        _bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        UITapGestureRecognizer *tapOpaqueViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
        [_bkView addGestureRecognizer:tapOpaqueViewGesture];
        [self insertSubview:_bkView atIndex:0];
    }
    return _bkView;
}

- (UIView *)contentView{
    if (!_contentView) {
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 230)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [_contentView addSubview:self.pickerToolBar];
        self.pickView.bottom = _contentView.height;
        [_contentView addSubview:self.pickView];
        [self addSubview:_contentView];
    }
    return _contentView;
}

- (UIToolbar *)pickerToolBar{
    if (!_pickerToolBar) {
        //toolbar
        self.pickerToolBar = [[UIToolbar alloc]  initWithFrame:CGRectMake(0, 0, self.width, 44)]; //创建工具条，用来设置或者退出actionsheet.
        _pickerToolBar.backgroundColor=[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1.0f];
        [_pickerToolBar sizeToFit];
        
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:ASLocalizedString(@"完成") style:UIBarButtonItemStylePlain target:self action:@selector(closePickView)];
        [barItems addObject:cancelButton];
        [_pickerToolBar setItems:barItems animated:YES]; //将按键加入toolbar
    }
    return _pickerToolBar;
}


- (UIPickerView *)pickView{
    if (!_pickView) {
        self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(75, 0, self.width - 150, 200)];
        _pickView.delegate = self;
        _pickView.dataSource = self;
        _pickView.backgroundColor = [UIColor clearColor];
        _pickView.showsSelectionIndicator = YES;
    }
    return _pickView;
}

@end
